<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'no',                       #订单编号
        'shop_id',                  #店铺id
        'user_id',                  #用户id
        'contact_name',             #联系人
        'contact_phone',            #联系人电话
        'schedule_id',              #预定时间id
        'total_amount',             #支付金额
        'paid_at',                  #支付时间
        'refund_status',            #退款状态
        'refund_at',                #退款时间
        'refund_verify',            #退款验证是否成功
        'status',                   #订单状态
        'closed',                   #订单是否关闭
        'remark',                   #备注
        'goods_id',                 #商品id
        'goods_type',               #商品类型
        'user_distance'             #用户距离订单约定地点距离
    ];

    protected $casts = [
        'closed' => 'boolean',
        'refund_verify' => 'boolean',
    ];

    const DISTANCE_STANDARD = 500;#订单中 用户距离服务地点标准

    const SELLER_VIEW_ORDER_STANDARD = 600;#订单中 商家查看订单时间标准 商家600秒后才能看到订单

    const EXPIRE_SECONDS = 900;#订单中 用户过期未收货时间标准(秒)

    const CLOSE_SECONDS = 1800;#订单中 订单未支付关闭时间标准(秒)

    const REFUND_DISCOUNT = 0.8;#退款折扣

    const REWARD_DISCOUNT = 0.8;#报酬折扣

    const REFUND_SECONDS = 86400;#24小时

    const  REFUND_STATUS_PENDING = 'pending';

    const REFUND_STATUS_APPLIED = 'applied';#用户申请退款

    const REFUND_STATUS_UNDER_REVIEW = 'under_review';

    const REFUND_STATUS_PROCESSING = 'processing';

    const REFUND_STATUS_SUCCESS = 'success';

    const REFUND_STATUS_FAILED = 'failed';

    const STATUS_PENDING = 'pending';

    const STATUS_PAID = 'paid';

    const STATUS_RECEIVED = 'received';

    const STATUS_SIGNED = 'signed';

    const STATUS_CONFIRM = 'confirm';

    const STATUS_FINISH = 'finish';

    const STATUS_FAILED = 'failed';

    const STATUS_USER_CANCEL = 'user_cancel';

    const STATUS_SELLER_CANCEL = 'seller_cancel';

    const STATUS_BOTH_CANCEL = 'both_cancel';

    const CLOSED = '訂單關閉';

    public static $REFUND_STATUS_MAP = [
        self::REFUND_STATUS_PENDING => '待退款',
        self::REFUND_STATUS_APPLIED => '申請退款',
        self::REFUND_STATUS_UNDER_REVIEW => '復審中',
        self::REFUND_STATUS_PROCESSING => '退款中',
        self::REFUND_STATUS_SUCCESS => '退款成功',
        self::REFUND_STATUS_FAILED => '退款失敗',
    ];

    public static $STATUS_MAP = [
        self::STATUS_PENDING => '未支付',
        self::STATUS_PAID => '已支付',
        self::STATUS_RECEIVED => '商家已接單',
        self::STATUS_SIGNED => '用戶簽到',
        self::STATUS_CONFIRM => '用戶已確認服務開始',
        self::STATUS_FINISH => '已完成',
        self::STATUS_USER_CANCEL => '用戶取消',
        self::STATUS_SELLER_CANCEL => '商家取消',
        self::STATUS_BOTH_CANCEL => '雙方取消',
        self::STATUS_FAILED => '支付失敗',
    ];

    public static $PROCEDURE_STEP_MAP = [
        self::STATUS_PENDING => 1,
        self::STATUS_PAID => 2,
        self::STATUS_FAILED => 2,
        self::STATUS_RECEIVED => 3,
        self::STATUS_SIGNED => 4,
        self::STATUS_CONFIRM => 5,
        self::STATUS_FINISH => 6,
        self::STATUS_USER_CANCEL => 2,
        self::STATUS_SELLER_CANCEL => 2,
        self::STATUS_BOTH_CANCEL => 2,
        self::REFUND_STATUS_APPLIED => 3,
        self::REFUND_STATUS_UNDER_REVIEW => 5,
        self::REFUND_STATUS_PROCESSING => 5,
        self::REFUND_STATUS_SUCCESS => 6,
        self::REFUND_STATUS_FAILED => 6,
    ];

    //数组中的状态将导致订单关闭
    public static $CLOSED_STATUS_MAP = [
        self::STATUS_USER_CANCEL,
        self::STATUS_SELLER_CANCEL,
        self::STATUS_BOTH_CANCEL,
        self::STATUS_FAILED,
    ];

    public static $CLOSED_MAP = [
        '否', '是',
    ];

    protected static function boot()
    {
        parent::boot();
        // 监听模型创建事件，在写入数据库之前触发
        static::creating(function ($model) {
            // 如果模型的 no 字段为空
            if (!$model->no) {
                // 调用 findAvailableNo 生成订单流水号
                $model->no = rand_no();
                // 如果生成失败，则终止创建订单
                if (!$model->no) {
                    return false;
                }
            }
        });
    }

    public function goods()
    {
        return $this->morphTo()->withDefault();
    }

    public function workOrders()
    {
        return $this->morphMany(HelpWorkOrder::class, 'object');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->withDefault();
    }

    public function schedule()
    {
        return $this->hasOne(Schedule::class, 'id', 'schedule_id')->withDefault();
    }
}
